/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;

/**
 *
 * @author Nguyen Trung
 */
public class Question {
    private String question;
    private String answer;

    public Question()
    {
        this.question ="";
        this.answer = "";
    }
    
    public static Question[] readQuestion(String path) {
        ArrayList list = new ArrayList();
        
       
        try {
            Question tmp = new Question();
            System.out.println(path);
            System.setProperty("file.encoding", "UTF-8");

            Reader reader = new InputStreamReader(new FileInputStream(path),"UTF-8");
            BufferedReader br = new BufferedReader(reader);
            String strLine;
            //Read File Line By Line
            int i = 0;
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                if (strLine.length() == 0) {
                    //System.out.println("trong");
                    i++;
                    if(i==0)
                    {
                        //tmp.setQuestion(strLine);
                        
                    }
                    else if(i%2==0)
                    {
                        list.add(tmp);
                        tmp = new Question();
                    }

                    continue;
                }
                if (i % 2 == 0) {
                    System.out.println("câu hỏi : " + strLine);
                    
                    tmp.setQuestion(strLine);
                } else {
                    System.out.println("câu trả lời : " + strLine);
                    if(tmp.getAnswer().length() > 0)
                    {
                        tmp.setAnswer(tmp.getAnswer()+"\n"+ strLine);
                    }
                    else {tmp.setAnswer(strLine);}
                    
                }
            }
            
            //Close the input stream
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        Question[] questions = new Question[list.size()];
        list.toArray(questions);
        
        return questions;
    }

    /**
     * @return the question
     */
    public String getQuestion() {
        return question;
    }

    /**
     * @param question the question to set
     */
    public void setQuestion(String question) {
        this.question = question;
    }

    /**
     * @return the answer
     */
    public String getAnswer() {
        return answer;
    }

    /**
     * @param answer the answer to set
     */
    public void setAnswer(String answer) {
        this.answer = answer;
    }
}
